from defs import *
from taskserial import TaskSerial
from tasketh import TaskEth
import Queue
import time

debug_level = 0
time_sleep = 0.5

CONN_NONE = 0
CONN_ETH  = 1
CONN_SER  = 2
CONN_BOTH = 3

conn_state = None
arduino    = None
master     = None


ser_q_rx = Queue.Queue()
ser_q_tx = Queue.Queue()
eth_q_rx = Queue.Queue()
eth_q_tx = Queue.Queue()

def log_debug(level,message):
    global debug_level
    if level>=debug_level:
        print message

def DoConnNone():
    global conn_state,arduino,master
    conn_state = conn_state
    if arduino.IsConnAlive():
        conn_state = CONN_SER
    if master.IsConnAlive():
        if conn_state==CONN_SER:
            conn_state = CONN_BOTH
        else:
            conn_state = CONN_ETH
    

def DoConnEth():
    global conn_state,arduino,master
    conn_state = conn_state
    if not master.IsConnAlive():
        conn_state = CONN_NONE
    else:
        #FIXME: Update eth tx
        x = master.CheckForRX() #FIXME: Update eth rx
        if arduino.IsConnAlive():
            conn_state = CONN_BOTH


def DoConnSer():
    global conn_state,arduino,master
    conn_state = conn_state
    if not arduino.IsConnAlive():
        conn_state = CONN_NONE
    else:
        #FIXME: Update ser tx
        #FIXME: Update ser rx
        if master.IsConnAlive():
            conn_state = CONN_BOTH
            
def DoConnBoth():
    global conn_state,eth_q_rx,eth_q_tx,ser_q_rx,ser_q_tx,master,arduino
    if arduino.IsConnAlive() and not master.IsConnAlive():
        conn_state = CONN_SER
    elif master.IsConnAlive() and not arduino.IsConnAlive():
        conn_state = CONN_ETH
    elif not arduino.IsConnAlive() and not master.IsConnAlive():
        conn_state = CONN_NONE
    else:
        x = master.CheckForRX() #FIXME: Update eth rx
        if not eth_q_rx.empty():
            log_debug(1,"sending message from eth to ser")
            ser_q_tx.put(eth_q_rx.get(0))
        #FIXME: Update ser tx
        #FIXME: Update ser rx
        if not ser_q_rx.empty():
            log_debug(1,"sending message from ser to eth")
            eth_q_tx.put(ser_q_rx.get(0))            
        #FIXME Update eth tx


if __name__ == "__main__":
    log_debug(0,"loading...")
    conn_state = CONN_NONE
    enable_run = True
    arduino = TaskSerial(ser_q_rx,ser_q_tx)
    arduino.Open("COM30",9600,1)
    master  = TaskEth(eth_q_rx,eth_q_tx)
    master.Create('localhost',10000)
    master.StartListen(1)
    
    log_debug(1,"Hit CTRL+C to exit")
    while enable_run:
        try:
            log_debug(0,"running in state "+str(conn_state))
            if conn_state == CONN_NONE:
                DoConnNone()
            elif conn_state == CONN_ETH:
                DoConnEth()
            elif conn_state == CONN_SER:
                DoConnSer()
            elif conn_state == CONN_BOTH:
                DoConnBoth()
            time.sleep(time_sleep)
        except (KeyboardInterrupt, SystemExit):
            enable_run = False
            log_debug(0,"stop running")


    
    log_debug(0,"program exiting...")
    exit(0)
    #-----------------------------
    #old code
    print PROTOSER.HLENGTH
    commeth=TaskETH('localhost',10000)
    commeth.StartListen(1)
    while 1:
        print "Check for new connection..."
        if commeth.CheckForConnection():
            print "New client connected."
            a,b = commeth.GetClientInfo()
            print str(a),str(b)
            while 1:
                if not commeth.IsConnected():
                    break
                isRx,msg = commeth.CheckForRX()
                if isRx:
                    print "RX: ", msg

                    

